var countGlo = 1
function showGoods(page) {
    var dataGoods = document.getElementById("dataGoods")
    var stotal = document.getElementById("stotal")
    var spage = document.getElementById("spage")
    spage.innerHTML = page
    $.ajax({

        url: "goods/list",
        data: {page},
        success(response){
            let cout = response.count
            countGlo = cout
            let goods = response.goods;
            stotal.innerHTML = cout
            let html =''
            for (let i = 0; i < goods.length; i++) {
                let g = goods[i]
                html += `
                <div class="col-md-4 nowback">
    <div class="thumbnail">

            <p style="font-size: 1.1em">我是${g.name}</p>
            <p>价格:<b style="color: #3e8f3e">${g.price}</b>￥</p>
            <p><a href="/goods/edit?id=${g.id}" class="btn btn-primary" role="button">编辑</a> <a href="/goods/delete?id=${g.id}" class="btn btn-default" role="button">删除</a></p>
        </div>
    </div>  
</div>
            `;
            }
            dataGoods.innerHTML = html
        }
    })
}


window.addEventListener("load",function () {
    showGoods(1)
})

function nextPage() {
    pageNum = parseInt(document.getElementById(
        "spage").innerText)
    pageNum += 1
    if (pageNum <= parseInt((countGlo%6)==0?countGlo/6:countGlo/6 + 1))
        showGoods(pageNum)
}

function lastPage() {
    pageNum = parseInt(document.getElementById("spage").innerText)
    if (pageNum < 1){
        document.getElementById("spage").innerHTML = 1
        return
    }else if (pageNum == 1){
        return
    }

    pageNum -= 1
    showGoods(pageNum)
}


function firstPage() {
    pageNum = parseInt(document.getElementById("spage").innerText)
    if (pageNum != 1){
        showGoods(1)
    }

}
function overPage() {
    pageNum = parseInt(document.getElementById("spage").innerText)
    if (pageNum != parseInt((countGlo%6)==0?countGlo/6:countGlo/6 + 1)){
        showGoods(parseInt((countGlo%6)==0?countGlo/6:countGlo/6 + 1))
    }

}

function removeAttr() {
    $.ajax({
        url: 'goods/remove',
        success(response) {
            location.href = "/goods/edit"
        }
    })
}